package com.jt.antiFakeSys.utils;

import cn.hutool.core.util.ImageUtil;
import cn.hutool.core.util.ZipUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import lombok.extern.slf4j.Slf4j;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;

/**
 * CodeImageUtils
 *
 * @author longhp
 * @Date 2019/3/2 17:56
 */
@Slf4j
public class CodeImageUtils {

    /**
     * 800像素的文字大小为53  “诚信编码 : 39444722505049685”
     */
    public static BufferedImage createTextImage(int width,String text,int fontSize){
        Font font=new Font("微软雅黑",Font.PLAIN,fontSize);
        //获取font的样式应用在str上的整个矩形
        Rectangle2D r=font.getStringBounds(text, new FontRenderContext(AffineTransform.getScaleInstance(1, 1),false,false));
        //获取单个字符的高度
        int unitHeight=(int)Math.floor(r.getHeight());
        //把单个字符的高度+3保证高度绝对能容纳字符串作为图片的高度
        int height=unitHeight+3;
        //创建图片
        BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_BGR);
        Graphics g=image.getGraphics();
        g.setColor(Color.WHITE);
        //先用白色填充整张图片,也就是背景
        g.fillRect(0, 0, width, height);
        //在换成黑色
        g.setColor(Color.black);
        //设置画笔字体
        g.setFont(font);
        //画出字符串
        g.drawString(text, 0,font.getSize());
        g.dispose();
        return image;
    }

    /**
     * 竖向合成
     * 注意：该方法合成图片需要图片的宽度一样,高度不一致也可以
     * （查阅网上知道的，实际上有时候就是不行，蓝瘦香菇。）
     */
    public static BufferedImage  jointPic(BufferedImage imgA , BufferedImage imgB ) {
        try {
            int w = imgA.getWidth();
            int h = imgA.getHeight();
            int x = imgB.getWidth();
            int y = imgB.getHeight();
            BufferedImage image=new BufferedImage(w, h+y,BufferedImage.TYPE_INT_BGR);
            Graphics2D g  = image.createGraphics();
            g.setColor(Color.WHITE);
            //先用白色填充整张图片,也就是背景
            g.fillRect(0, 0, w, h+y);
            g.drawImage(imgA, 0, 0, w, h, null);
            g.drawImage(imgB, 0, h, x, y, null);
            g.dispose();
            return image;
        } catch (Exception e) {
            log.error("图片合成失败。。");
            e.printStackTrace();
        }
        return null;
    }



    public static void main(String[] args) throws Exception {
        QrConfig qrConfig = new QrConfig(800, 800);
        qrConfig.setErrorCorrection(ErrorCorrectionLevel.L);
        qrConfig.setMargin(1);
//        BufferedImage bufferedImage =  QrCodeUtil.generate("http://hualin.cxbz.org/index.php?s=LabelInfo/viewInfo/id/41368849121289821",qrConfig);
        BufferedImage bufferedImage =  QrCodeUtil.generate("http://192.168.1.107:6088/index.php?s=LabelInfo/viewInfo/id/39444722505049685",qrConfig);
        bufferedImage = ImageUtil.rotate(bufferedImage,90);
        BufferedImage bufferedImage2 = createTextImage(800, "诚信编码 : 41368849121289821", 53);
        ImageIO.write( jointPic(bufferedImage,bufferedImage2),"png",new File("D:\\test\\66.png"));

//        ZipUtil.zip("D:\\test");
    }
}
